var jqTabs = function(options){
	this.jqOpts = options;
	this.prevObj = null;
	this.prevIndex = -1;
	this.currentIndex = this.jqOpts.defaultIndex;
	this.objList = $(this.jqOpts.selector+" > ul").children("li");
};

jqTabs.prototype.getWidget = function(){
	$(this.jqOpts.selector).tabs({ 
		active: this.currentIndex/*,
		beforeActivate: function( event, ui ) {
			$(ui.oldTab[0]).children("a").blur();
			$(ui.newTab[0]).children("a").blur();
		},
		activate: function( event, ui ) {			
			$(ui.oldTab[0]).children("a").blur();
			$(ui.newTab[0]).children("a").blur();
		}*/
	});
	
	this.setPrevObj($(this.objList).eq(this.currentIndex));	
	this.exeAction(this.prevObj);
};

jqTabs.prototype.setPrevObj = function(obj){
	this.prevObj = obj;
};

jqTabs.prototype.setPrevIndex = function(val){
	this.prevIndex = val;
};

jqTabs.prototype.setCurrIndex = function(){
	this.currentIndex = $(this.jqOpts.selector).tabs("option", "active");
};

jqTabs.prototype.detectUrl = function(obj){
	var url = $(obj).children("span").text();
	if(url.replace(/ /g, "") == "" || url == "undefined"){
		url = "";
	}	
	return url;
};

jqTabs.prototype.exeAction = function(obj){
	this.setCurrIndex();
	if(this.prevIndex == this.currentIndex) return;

	var url = this.detectUrl($(obj));
	if(url != ""){ 
		loadTabsContainer(url,"POST","html",$(obj), this);
	}else{
		this.setPrevObj($(obj));
		this.setPrevIndex(this.currentIndex);
	}
};

var loadTabsContainer = function(path, mType, dtype, obj, widget) {
	var ajax = new jqMyAjax();
	var data = "";
	var option = new Array();
	option["type"] = mType;
	option["url"] = path;
	option["dataType"] = dtype;
	option["data"] = data;
	option["success"] = function(result){
		widget.setPrevObj($(obj));
		widget.setPrevIndex(widget.currentIndex);
		
		$(widget.jqOpts.selector + " > " + widget.jqOpts.content).html(result);
	};
	option["error"] = function(xhr, status, thrown){
		var extraMsg = "\n\nThis tab selected is disabled.";
		var sessionOutRedirect = true;
		ajax.errorCalback(xhr, extraMsg, sessionOutRedirect);
		
		$(widget.jqOpts.selector).tabs( "disable", widget.currentIndex);
		$(widget.jqOpts.selector).tabs("option", "active", widget.prevIndex);
		widget.exeAction($(widget.prevObj));
	};

	ajax.callAjax(option);
};
/*
//## jQuery UI Tabs Usage.
$(selector).tabs();
$(selector).tabs({ active: 0 });//Initialize
var active = $(selector).tabs( "option", "active" );//getter
$(selector).tabs( "option", "active", 1 );//setter
*/
/*$(selector).tabs({ collapsible: true });
var collapsible = $(selector).tabs( "option", "collapsible" );
$(selector).tabs( "option", "collapsible", true );*/

/*$(selector).tabs({ disabled: [ 0, 2 ] });
var disabled = $(selector).tabs( "option", "disabled" );
$(selector).tabs( "option", "disabled", [ 0, 2 ] );*/

/*$(selector).tabs({ event: "mouseover" });
var event = $(selector).tabs( "option", "event" );
$(selector).tabs( "option", "event", "mouseover" );*/

/*$(selector).tabs({ heightStyle: "fill" });
var heightStyle = $(selector).tabs( "option", "heightStyle" );
$(selector).tabs( "option", "heightStyle", "fill" );*/

/*$(selector).tabs({ hide: { effect: "explode", duration: 1000 } });
var hide = $(selector).tabs( "option", "hide" );
$(selector).tabs( "option", "hide", { effect: "explode", duration: 1000 } );*/

/*$(selector).tabs({ show: { effect: "blind", duration: 800 } });
var show = $(selector).tabs( "option", "show" );
$(selector).tabs( "option", "show", { effect: "blind", duration: 800 } );*/

/*$(selector).tabs( "destroy" );*/
/*$(selector).tabs( "disable" );*/
/*$(selector).tabs( "disable", 1 );*/
/*$(selector).tabs( "enable" );*/
/*$(selector).tabs( "enable", 1 );*/